package com.aidong.activity.biz.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.aidong.activity.biz.MstCampaignBiz;
import com.aidong.activity.dao.MstCampaignDao;
import com.aidong.activity.entity.Home;
import com.aidong.activity.entity.Member;
import com.aidong.activity.entity.MstCampaign;
import com.aidong.food.dao.TrnFoodBuyDao;
import com.aidong.myprofile.dao.MyAddressDao;
import com.aidong.myprofile.dao.MyAppointmentDao;
import com.aidong.pay.dao.OrderManagementDao;
import com.aidong.pay.dao.OrdergeneratedDao;
import com.aidong.util.bus.CodeConsts;
import com.aidong.util.bus.SecurityUtils;

@Service("mstCampaignBiz")
public class MstCampaignBizImpl implements MstCampaignBiz {

	private MstCampaignDao mstCampaignDao;
	
	private MyAppointmentDao appointmentDao;
	
	private TrnFoodBuyDao trnFoodBuyDao;
	
	private OrderManagementDao orderManagementDao;
	
	private OrdergeneratedDao ordergeneratedDao;
	
	private MyAddressDao myAddressDao;
	
	@Autowired
	public void setMyAddressDao(MyAddressDao myAddressDao) {
		this.myAddressDao = myAddressDao;
	}
	
	@Autowired
	public void setOrdergeneratedDao(OrdergeneratedDao ordergeneratedDao) {
		this.ordergeneratedDao = ordergeneratedDao;
	}
	
	@Autowired
	public void setOrderManagementDao(OrderManagementDao orderManagementDao) {
		this.orderManagementDao = orderManagementDao;
	}
	
	@Autowired
	public void setAppointmentDao(MyAppointmentDao appointmentDao) {
		this.appointmentDao = appointmentDao;
	}

	@Autowired
	public void setMstCampaignDao(MstCampaignDao mstCampaignDao) {
		this.mstCampaignDao = mstCampaignDao;
	}
	
	@Autowired
	public void setTrnFoodByDao(TrnFoodBuyDao trnFoodBuyDao) {
		this.trnFoodBuyDao = trnFoodBuyDao;
	}

	/**
	 * 查找所有活动
	 * @param map
	 * @return
	 */
	@Override
	public List<Map<String, Object>> getMstCampaign(Map<String, Object> map) {
		return mstCampaignDao.getMstCampaign(map);
	}

	/**
	 * 当前日期距离活动状态天数
	 * @param map
	 * @return
	 */
	@Override
	public int getMstCampaignDay(String dateTime) {
		return mstCampaignDao.getMstCampaignDay(dateTime);
	}

	/**
	 * 活动详情
	 */
	@Override
	public List<Map<String, Object>> getMstCampaignDetail(int campaignId) {
		return mstCampaignDao.getMstCampaignDetail(campaignId);
	}

	/**
	 * 添加活动报名
	 * @param campaign
	 */
	@Override
	public void addActivityReg(MstCampaign campaign) {
		mstCampaignDao.addActivityReg(campaign);
		// mstCampaignDao.editCampaignSignUpCount(campaign.getCampaignId());
		// 预约标记位
		Map<String, Object> paraMap = new HashMap<String, Object>();
		paraMap.put("status", 0);
		paraMap.put("idongId", campaign.getIdongId());
		if (campaign.getMovieFlg() == 0) {
			appointmentDao.eidtMemberAppointFlg(paraMap);//我的预约标记
		}else if (campaign.getMovieFlg() == 3) {
			//我的订单标记
			trnFoodBuyDao.editBuyFoodFlg(paraMap);
		}
		
	}

	/**
	 * 如果报名之前为注册，先注册
	 * @param map
	 * @return
	 */
	@Override
	public int addMemberReg(Map<String, Object> map) {
		return mstCampaignDao.addMemberReg(map);
	}

	/**
	 * 如果报名之前已注册，查找已注册信息
	 * @param idongId
	 * @return
	 */
	@Override
	public Map<String, Object> getMemberReg(int idongId) {
		return mstCampaignDao.getMemberReg(idongId);
	}

	/**
	 * 判断该手机号码是否已注册
	 * @param phoneNo
	 * @return
	 */
	@Override
	public int getIfRegister(String phoneNo) {
		return mstCampaignDao.getIfRegister(phoneNo);
	}

	/**
	 * 根据手机号码获得爱动号
	 * @param phone
	 * @return
	 */
	@Override
	public int getIdongIdByPhone(String phone) {
		return mstCampaignDao.getIdongIdByPhone(phone);
	}

	/**
	 * 判断是否已经报名
	 * @param map
	 * @return
	 */
	@Override
	public List<Map<String, Object>> getIfAlreadyReg(Map<String, Object> map) {
		return mstCampaignDao.getIfAlreadyReg(map);
	}

	/**
	 * 活动照片回顾
	 * @param campaignId
	 * @return
	 */
	@Override
	public List<Map<String, Object>> getCampaignReview(int campaignId) {
		return mstCampaignDao.getCampaignReview(campaignId);
	}

	/**
	 * 获得活动报名结束时间、报名人数、可参加人数
	 * @param campaignId
	 * @return
	 */
	@Override
	public Map<String, Object> getCampaignSignMsg(int campaignId) {
		return mstCampaignDao.getCampaignSignMsg(campaignId);
	}

	/**
	 * 活动报名会员列表
	 * @param campaignId
	 * @return
	 */
	@Override
	public List<Member> getAlreadySignedPerson(int campaignId) {
		return mstCampaignDao.getAlreadySignedPerson(campaignId);
	}

	/**
	 * 是否已关注对方爱动号
	 * @param map
	 */
	@Override
	public int getIfAttention(Map<String, Object> map) {
		return mstCampaignDao.getIfAttention(map);
	}

	/**
	 * 内容推荐
	 * @return
	 */
	@Override
	public List<Home> getCampaignBanner(Map<String, Object> map) {
		return mstCampaignDao.getCampaignBanner(map);
	}

	/**
	 * 是否已经点赞
	 * @param map
	 */
	@Override
	public int getIfCampaignPrasie(Map<String, Object> map) {
		return mstCampaignDao.getIfCampaignPrasie(map);
	}

	/**
	 * 添加点赞记录
	 * 修改点赞人数
	 * @param map
	 */
	@Override
	public void addMstCampaignPrasie(Map<String, Object> map) {
		mstCampaignDao.addMstCampaignPrasie(map);
		mstCampaignDao.editMstCampaignCount(Integer.parseInt(map.get("campaignId").toString()));
	}

	/**
	 * 修改报名状态
	 * @param map
	 */
	@Override
	public void editCampaignSignUpStatus(Map<String, Object> map) {
		mstCampaignDao.editCampaignSignUpStatus(map);
		mstCampaignDao.editCampaignSignUpCount(Integer.parseInt(map.get("campaignId").toString()));
	}

	/**
	 * 活动报名有效人数
	 * @param campaignId
	 * @return
	 */
	@Override
	public int getCampaignValidCount(int campaignId) {
		return mstCampaignDao.getCampaignValidCount(campaignId);
	}

	/**
	 * 活动介绍
	 * @param campaignId
	 * @return
	 */
	@Override
	public Map<String, Object> getCampaignIntroduce(int campaignId) {
		mstCampaignDao.editMstCampaignNess(campaignId);
		return mstCampaignDao.getCampaignIntroduce(campaignId);
	}

	/**
	 * 查找已结束的活动
	 * @return
	 */
	@Override
	public List<Map<String, Object>> getEndCampaign(Map<String, Object> map) {
		return mstCampaignDao.getEndCampaign(map);
	}

	/**
	 * 活动回顾详情
	 * @param campaignId
	 * @return
	 */
	@Override
	public Map<String, Object> getMstCampaignReviewDuce(int campaignId) {
		return mstCampaignDao.getMstCampaignReviewDuce(campaignId);
	}

	/**
	 * 活动点赞人数
	 * @param campaignId
	 * @return
	 */
	@Override
	public int editMstCampaignNess(int campaignId) {
		return mstCampaignDao.editMstCampaignNess(campaignId);
	}

	/**
	 * 餐饮图片入口
	 * @return
	 */
	@Override
	public Map<String, Object> getFoodEntryImage() {
		return mstCampaignDao.getFoodEntryImage();
	}

	/**
	 * 广告位信息
	 * @return
	 */
	@Override
	public List<Map<String, Object>> getAdPositionIdInfo() {
		return mstCampaignDao.getAdPositionIdInfo();
	}

	/**
	 * 得到支付信息
	 * @param campaignId
	 * @return
	 */
	@Override
	public String getPayInfo(int campaignId) {
		return mstCampaignDao.getPayInfo(campaignId);
	}
	
	@Override
	public int ifHasMoviceTickets(int campaignId) {
		// TODO Auto-generated method stub
		return mstCampaignDao.ifHasMoviceTickets(campaignId);
	}

	@Override
	public void setMovieTicketsUse(Map<String, Object> map) {
		// TODO Auto-generated method stub
		mstCampaignDao.setMovieTicketsUse(map);
	}

	@Override
	public Map<String, Object> getMoviceBuyNum(int campaignId) {
		// TODO Auto-generated method stub
		return mstCampaignDao.getMoviceBuyNum(campaignId);
	}

	@Override
	public int getUserBuyMoviceCount(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return mstCampaignDao.getUserBuyMoviceCount(map);
	}

	@Override
	public void signPersonIsFull(Map<String, Object> map) {
		// TODO Auto-generated method stub
		mstCampaignDao.signPersonIsFull(map);
	}

	@Override
	public List<Map<String, Object>> getMstMatch(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return mstCampaignDao.getMstMatch(map);
	}

	@Override
	public List<Map<String, Object>> getEventAreas(int campaignId) {
		// TODO Auto-generated method stub
		return mstCampaignDao.getEventAreas(campaignId);
	}

	@Override
	public List<Map<String, Object>> getEventAreaRegion(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return mstCampaignDao.getEventAreaRegion(map);
	}

	@Override
	public List<Map<String, Object>> getEventAreaStore(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return mstCampaignDao.getEventAreaStore(map);
	}

	@Override
	public Map<String, Object> getOneCampaignInfo(int campaignId) {
		// TODO Auto-generated method stub
		return mstCampaignDao.getOneCampaignInfo(campaignId);
	}

	@Override
	public List<Map<String, Object>> getOneStoreMatchInfo(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return mstCampaignDao.getOneStoreMatchInfo(map);
	}

	@Override
	public int getMatchNumOnePerson(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return mstCampaignDao.getMatchNumOnePerson(map);
	}

	@Override
	public int getHasBuyTicketCount(int campaignId) {
		// TODO Auto-generated method stub
		return mstCampaignDao.getHasBuyTicketCount(campaignId);
	}

	@Override
	public List<Map<String, Object>> getOpenCity() {
		// TODO Auto-generated method stub
		return mstCampaignDao.getOpenCity();
	}
	/**
	 * 新版内容推荐
	 * @return
	 */
	public List<Home> getCampaignBannerNew(Map<String, Object> map){
		return mstCampaignDao.getCampaignBannerNew(map);
	}
	
	/**
	 * 根据活动ID获取该活动是否报名已满
	 */
	public MstCampaign  getFullByCampaignId(Map<String, Object> map){
		return mstCampaignDao.getFullByCampaignId(map);
	}
	
	/**
	 * 根据活动ID修改活动表中报名已满字段
	 * @param map
	 * @return
	 */
	public int updateFullByCampaignId(Map<String, Object> map){
		return mstCampaignDao.updateFullByCampaignId(map);
	}
	
	
	/**
	 * 根据嘉年华总获取嘉年华总的信息
	 * @param map
	 * @return
	 */
	public MstCampaign getCarnivalByType(Map<String, Object> map){
		return mstCampaignDao.getCarnivalByType(map);
	}
	
	public List<Map<String,Object>> getCarnivalIfAlreadyReg(Map<String, Object> map){
		return mstCampaignDao.getCarnivalIfAlreadyReg(map);
	}
	
	/**
	 * 根据地址ID获取地址信息
	 * @param addressId
	 * @return
	 */
	public Map<String,Object> getAddressDetailById(int addressId){
		return mstCampaignDao.getAddressDetailById(addressId);
	}
	
	/**
	 * 获取爱动训练营活动
	 * @return
	 */
	public List<MstCampaign> getCampaignAiDongList(int campaignType){
		return mstCampaignDao.getCampaignAiDongList(campaignType);
	}
	/**
	 * 报名
	 */
	@Transactional
	public Map<String,Object> editCampaignDetail(MstCampaign campaign){
		Map<String,Object> map=new HashMap<String,Object>();
		try {
			// 是否电影
			int movieFlg = campaign.getMovieFlg();
			campaign.setIdongId(SecurityUtils.decryptIdongId(campaign.getIdongIdEncrypt()) + ""); // 爱动号
			// 解密爱动号
			String idongId = campaign.getIdongId(); // 爱动号
			int campaignId = campaign.getCampaignId(); // 活动ID
			String contactName = campaign.getContactName(); // 联系人姓名
			// 解密联系人电话
			campaign.setContactPhone(SecurityUtils.decryptPhoneNo(campaign.getContactPhoneEncrypt()));
			String contactPhone = campaign.getContactPhone(); // 联系人电话

			if (campaignId > 0 && (!"".equals(contactName) && contactName != null)
					&& (!"".equals(contactPhone) && contactPhone != null)) {
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
				String sysDate = sdf.format(new Date());
				Date formatDate = sdf.parse(sysDate); // 系统当前年月日

				// 获得活动报名结束时间、报名人数、可参加人数
				Map<String, Object> msgMap = mstCampaignDao.getCampaignSignMsg(campaignId);
				int version=Integer.parseInt(msgMap.get("version").toString());//version 字段
				String signStratTime = msgMap.get("stratSignTime").toString(); // 活动报名开始时间
				Date stratDateTime = sdf.parse(signStratTime);
				String signEndTime = msgMap.get("endSignTime").toString(); // 活动报名截止时间
				Date endDateTime = sdf.parse(signEndTime);
				int brandId = Integer.valueOf(msgMap.get("brandId").toString());
				int allowPerson = Integer.parseInt(msgMap.get("allowPerson").toString()); // 活动可报名人数
				if (movieFlg == 0) {// 活动
					int signPerson = mstCampaignDao.getCampaignValidCount(campaignId); // 活动报名有效人数（已报名人数）

					// add 2016-02-18 调用存储过程判断是否可以报名需要的参数
					Map<String, Object> paraMap1 = new HashMap<String, Object>();
					paraMap1.put("campaignId", campaignId);
					paraMap1.put("allowPersonLocal", allowPerson);
					paraMap1.put("version", version);
					if (allowPerson > signPerson) {
						if (formatDate.compareTo(endDateTime) == 1 || formatDate.compareTo(stratDateTime) == -1) {
							map.put("code", 0);
							map.put("msg", "当前时间不在报名时间范围之内");
							return map;
						} else {
							if (!"".equals(idongId) && idongId != null && !"0".equals(idongId)) {// 已登录
								// 判断该手机是否已经报名
								Map<String, Object> paraMap = new HashMap<String, Object>();
								paraMap.put("campaignId", campaignId);
								paraMap.put("idongId", idongId);
								paraMap.put("version", version);
								List<Map<String, Object>> alreayMap = mstCampaignDao.getIfAlreadyReg(paraMap);
								if (alreayMap.size() > 0) { // 是否已有报名记录
									for (Map<String, Object> alreay : alreayMap) {
										int alreayStatus = Integer.parseInt(alreay.get("status").toString());
										if (alreayStatus == CodeConsts.CODE_STATIC_BOOKING_BOOKING) { // 已预约状态
											map.put("code", 0);
											map.put("msg", "您已报名该活动，请到订单列表查看");
											return map;
										}
									}
									Map<String,Object> para=new HashMap<String,Object>();
									para.put("campaignId",campaignId);
									MstCampaign campaignDetail=mstCampaignDao.getFullByCampaignId(para);
									if(campaignDetail ==null ){
										throw new Exception();
									}else if(Integer.parseInt(campaignDetail.getIsFull())==1){
										map.put("code", 0);
										map.put("msg", "预约人数已满");
										return map;
									}
									// 判断报名人数是否已满，更新是否成功
									// 若更新成功说明没满，否则人员已满，提示不可报名。
									// 调用存储过程
									
									paraMap.put("signPerson", signPerson);
									paraMap.put("allowPerson", allowPerson);
									
									int result=mstCampaignDao.updateFullByCampaignId(paraMap);
									if(result==0){
										map.put("code", 0);
										map.put("msg","当前预约人数较多，请稍后重试!");
										return map;
									}
									
									/*if(signPerson+1>=allowPerson){
										int result=mstCampaignDao.updateFullByCampaignId(paraMap);
										if(result==0){
											map.put("code", 0);
											map.put("msg","当前预约人数较多，请稍后重试!");
											return map;
										}
									}*/
									/*mstCampaignBiz.signPersonIsFull(paraMap1);
									if ((Integer) paraMap1.get("updateResult") == 0) {
										map.put("code", 0);
										map.put("msg", "报名人数已满");
									} else {*/
										// 添加预约信息
										mstCampaignDao.addActivityReg(campaign);
										int subscribeId = campaign.getSubscribeId(); // 预约ID

										// 添加付款信息
										Map<String, Object> payMap = new HashMap<String, Object>();
										payMap.put("subscribeId", subscribeId);
										payMap.put("orderType", 1);
										Map<String, Object> priceMap = ordergeneratedDao.getActivityPrice(subscribeId);
										payMap.put("amount", priceMap.get("price"));
										ordergeneratedDao.addCampaginOrderPay(payMap);
										String orderId = payMap.get("orderId").toString();

										map.put("code", 1);
										map.put("subscribeId", subscribeId);
										map.put("orderId", orderId);
										List<Map<String, Object>> payInfo = getPayInfo(
												mstCampaignDao.getPayInfo(campaignId));
										map.put("zhifubao", payInfo.get(0).get("zhifubao"));
										map.put("weixin", payInfo.get(1).get("weixin"));
										map.put("voucher", payInfo.get(2).get("voucher"));
										map.put("exchange", payInfo.get(3).get("exchange"));
										map.put("membership", payInfo.get(4).get("membership"));
										map.put("discount", payInfo.get(5).get("discount"));
									//}
									return map;
								} else {
									Map<String,Object> para=new HashMap<String,Object>();
									para.put("campaignId",campaignId);
									MstCampaign campaignDetail=mstCampaignDao.getFullByCampaignId(para);
									if(campaignDetail ==null ){
										throw new Exception();
									}else if(Integer.parseInt(campaignDetail.getIsFull())==1){
										map.put("code", 0);
										map.put("msg", "预约人数已满");
										return map;
									}
									paraMap.put("signPerson", signPerson);
									paraMap.put("allowPerson", allowPerson);
									
									int result=mstCampaignDao.updateFullByCampaignId(paraMap);
									if(result==0){
										map.put("code", 0);
										map.put("msg","当前预约人数较多，请稍后重试!");
										return map;
									}
										/*if(signPerson+1>=allowPerson){
											int result=mstCampaignDao.updateFullByCampaignId(paraMap);
											if(result==0){
												map.put("code", 0);
												map.put("msg","当前预约人数较多，请稍后重试!");
												return map;
											}
										}*/
									
									/*mstCampaignBiz.signPersonIsFull(paraMap1);
									if ((Integer) paraMap1.get("updateResult") == 0) {
										map.put("code", 0);
										map.put("msg", "报名人数已满");
									} else {*/

										mstCampaignDao.addActivityReg(campaign);
										int subscribeId = campaign.getSubscribeId(); // 预约ID

										// 添加付款信息
										Map<String, Object> payMap = new HashMap<String, Object>();
										payMap.put("subscribeId", subscribeId);
										payMap.put("orderType", 1);
										Map<String, Object> priceMap = ordergeneratedDao.getActivityPrice(subscribeId);
										payMap.put("amount", priceMap.get("price"));
										ordergeneratedDao.addCampaginOrderPay(payMap);
										String orderId = payMap.get("orderId").toString();

										map.put("code", 1);
										map.put("subscribeId", subscribeId); // 预约ID
										map.put("orderId", orderId); // 订单ID
										List<Map<String, Object>> payInfo = getPayInfo(
												mstCampaignDao.getPayInfo(campaignId));
										map.put("zhifubao", payInfo.get(0).get("zhifubao"));
										map.put("weixin", payInfo.get(1).get("weixin"));
										map.put("voucher", payInfo.get(2).get("voucher"));
										map.put("exchange", payInfo.get(3).get("exchange"));
										map.put("membership", payInfo.get(4).get("membership"));
										map.put("discount", payInfo.get(5).get("discount"));
									//}
									return map;
								}
							} else {// 未登录
									// 判断该手机号码是否已注册
								int count = mstCampaignDao.getIfRegister(contactPhone);
								if (count > 0) { // 已注册
									// 判断该手机是否已经报名
									int rsultIdongId = mstCampaignDao.getIdongIdByPhone(contactPhone);
									Map<String, Object> paraMap = new HashMap<String, Object>();
									paraMap.put("campaignId", campaignId);
									paraMap.put("idongId", rsultIdongId);
									paraMap.put("version", version);
									List<Map<String, Object>> alreayMap = mstCampaignDao.getIfAlreadyReg(paraMap);
									if (alreayMap.size() > 0) {
										for (Map<String, Object> alreay : alreayMap) {
											int alreayStatus = Integer.parseInt(alreay.get("status").toString());
											if (alreayStatus == CodeConsts.CODE_STATIC_BOOKING_BOOKING) { // 已报名状态
												map.put("code", 0);
												map.put("msg", "您已报名该活动");
												return map;
											}
										}

										Map<String,Object> para=new HashMap<String,Object>();
										para.put("campaignId",campaignId);
										MstCampaign campaignDetail=mstCampaignDao.getFullByCampaignId(para);
										if(campaignDetail ==null ){
											throw new Exception();
										}else if(Integer.parseInt(campaignDetail.getIsFull())==1){
											map.put("code", 0);
											map.put("msg", "预约人数已满");
											return map;
										}
										paraMap.put("signPerson", signPerson);
										paraMap.put("allowPerson", allowPerson);
										
										int result=mstCampaignDao.updateFullByCampaignId(paraMap);
										if(result==0){
											map.put("code", 0);
											map.put("msg","当前预约人数较多，请稍后重试!");
											return map;
										}
										/*if(signPerson+1>=allowPerson){
											int result=mstCampaignDao.updateFullByCampaignId(paraMap);
											if(result==0){
												map.put("code", 0);
												map.put("msg","当前预约人数较多，请稍后重试!");
												return map;
											}
										}*/
										/*mstCampaignBiz.signPersonIsFull(paraMap1);
										if ((Integer) paraMap1.get("updateResult") == 0) {
											map.put("code", 0);
											map.put("msg", "报名人数已满");
										} else {*/
											campaign.setIdongId(rsultIdongId + "");
											mstCampaignDao.addActivityReg(campaign);
											int subscribeId = campaign.getSubscribeId(); // 预约ID

											// 添加付款信息
											Map<String, Object> payMap = new HashMap<String, Object>();
											payMap.put("subscribeId", subscribeId);
											payMap.put("orderType", 1);
											Map<String, Object> priceMap = ordergeneratedDao
													.getActivityPrice(subscribeId);
											payMap.put("amount", priceMap.get("price"));
											ordergeneratedDao.addCampaginOrderPay(payMap);
											String orderId = payMap.get("orderId").toString();

											map.put("code", 1);
											map.put("subscribeId", subscribeId); // 预约ID
											map.put("orderId", orderId); // 订单ID
											List<Map<String, Object>> payInfo = getPayInfo(
													mstCampaignDao.getPayInfo(campaignId));
											map.put("zhifubao", payInfo.get(0).get("zhifubao"));
											map.put("weixin", payInfo.get(1).get("weixin"));
											map.put("voucher", payInfo.get(2).get("voucher"));
											map.put("exchange", payInfo.get(3).get("exchange"));
											map.put("membership", payInfo.get(4).get("membership"));
											map.put("discount", payInfo.get(5).get("discount"));
										//}
										return map;
									} else {// 未报名
										Map<String,Object> para=new HashMap<String,Object>();
										para.put("campaignId",campaignId);
										MstCampaign campaignDetail=mstCampaignDao.getFullByCampaignId(para);
										if(campaignDetail ==null ){
											throw new Exception();
										}else if(Integer.parseInt(campaignDetail.getIsFull())==1){
											map.put("code", 0);
											map.put("msg", "预约人数已满");
											return map;
										}
										paraMap.put("signPerson", signPerson);
										paraMap.put("allowPerson", allowPerson);
										
										int result=mstCampaignDao.updateFullByCampaignId(paraMap);
										if(result==0){
											map.put("code", 0);
											map.put("msg","当前预约人数较多，请稍后重试!");
											return map;
										}
										/*if(signPerson+1>=allowPerson){
											int result=mstCampaignDao.updateFullByCampaignId(paraMap);
											if(result==0){
												map.put("code", 0);
												map.put("msg","当前预约人数较多，请稍后重试!");
												return map;
											}
										}*/

										/*mstCampaignBiz.signPersonIsFull(paraMap1);
										if ((Integer) paraMap1.get("updateResult") == 0) {
											map.put("code", 0);
											map.put("msg", "报名人数已满");
										} else {*/
											campaign.setIdongId(rsultIdongId + "");
											mstCampaignDao.addActivityReg(campaign);
											int subscribeId = campaign.getSubscribeId(); // 预约ID

											// 添加付款信息
											Map<String, Object> payMap = new HashMap<String, Object>();
											payMap.put("subscribeId", subscribeId);
											payMap.put("orderType", 1);
											Map<String, Object> priceMap = ordergeneratedDao
													.getActivityPrice(subscribeId);
											payMap.put("amount", priceMap.get("price"));
											ordergeneratedDao.addCampaginOrderPay(payMap);
											String orderId = payMap.get("orderId").toString();

											map.put("code", 1);
											map.put("subscribeId", subscribeId); // 预约ID
											map.put("orderId", orderId); // 订单ID
											List<Map<String, Object>> payInfo = getPayInfo(
													mstCampaignDao.getPayInfo(campaignId));
											map.put("zhifubao", payInfo.get(0).get("zhifubao"));
											map.put("weixin", payInfo.get(1).get("weixin"));
											map.put("voucher", payInfo.get(2).get("voucher"));
											map.put("exchange", payInfo.get(3).get("exchange"));
											map.put("membership", payInfo.get(4).get("membership"));
											map.put("discount", payInfo.get(5).get("discount"));
										//}
										return map;
									}
								} else { // 未注册
									
									Map<String,Object> para=new HashMap<String,Object>();
									para.put("campaignId",campaignId);
									MstCampaign campaignDetail=mstCampaignDao.getFullByCampaignId(para);
									if(campaignDetail ==null ){
										throw new Exception();
									}else if(Integer.parseInt(campaignDetail.getIsFull())==1){
										map.put("code", 0);
										map.put("msg", "预约人数已满");
										return map;
									}
									paraMap1.put("signPerson", signPerson);
									paraMap1.put("allowPerson", allowPerson);
									
									int result=mstCampaignDao.updateFullByCampaignId(paraMap1);
									if(result==0){
										map.put("code", 0);
										map.put("msg","当前预约人数较多，请稍后重试!");
										return map;
									}
									/*if(signPerson+1>=allowPerson){
										int result=mstCampaignDao.updateFullByCampaignId(paraMap1);
										if(result==0){
											map.put("code", 0);
											map.put("msg","当前预约人数较多，请稍后重试!");
											return map;
										}
									}*/
									/*mstCampaignBiz.signPersonIsFull(paraMap1);
									if ((Integer) paraMap1.get("updateResult") == 0) {
										map.put("code", 0);
										map.put("msg", "报名人数已满");
									} else {*/
										// 先注册新用户，产生爱动号
										Map<String, Object> regMap = new HashMap<String, Object>();
										regMap.put("contactName", contactName);
										regMap.put("contactPhone", contactPhone);
										mstCampaignDao.addMemberReg(regMap);
										// 注册成功后产生爱动号
										String regIdongId = regMap.get("idongId").toString();
										campaign.setIdongId(regIdongId);
										// 保存报名信息
										mstCampaignDao.addActivityReg(campaign);
										int subscribeId = campaign.getSubscribeId(); // 预约ID

										// 添加付款信息
										Map<String, Object> payMap = new HashMap<String, Object>();
										payMap.put("subscribeId", subscribeId);
										payMap.put("orderType", 1);
										Map<String, Object> priceMap = ordergeneratedDao.getActivityPrice(subscribeId);
										payMap.put("amount", priceMap.get("price"));
										ordergeneratedDao.addCampaginOrderPay(payMap);
										String orderId = payMap.get("orderId").toString();

										map.put("code", 1);
										map.put("subscribeId", subscribeId); // 预约ID
										map.put("orderId", orderId); // 订单ID
										List<Map<String, Object>> payInfo = getPayInfo(
												mstCampaignDao.getPayInfo(campaignId));
										map.put("zhifubao", payInfo.get(0).get("zhifubao"));
										map.put("weixin", payInfo.get(1).get("weixin"));
										map.put("voucher", payInfo.get(2).get("voucher"));
										map.put("exchange", payInfo.get(3).get("exchange"));
										map.put("membership", payInfo.get(4).get("membership"));
										map.put("discount", payInfo.get(5).get("discount"));
									//}
									return map;
								}
							}
						}
					} else if (allowPerson <= signPerson) {
						map.put("code", 0);
						map.put("msg", "报名人数已满");
						return map;
					}
				} else if (movieFlg == 3) {// 票务
					double amount;
					// 根据活动ID拿到活动信息，判断可购买虚拟码数或者票务数
					Map<String, Object> matchCountMap = mstCampaignDao.getMoviceBuyNum(campaignId);
					Map<String, Object> paramMap = new HashMap<String, Object>();
					paramMap.put("idongId", Integer.valueOf(idongId));
					paramMap.put("campaignId", campaignId);

					if (matchCountMap.get("movieNum") != null) {
						int movieNum = (Integer) matchCountMap.get("movieNum");// 个人可购数
						int quantity = campaign.getQuantity();
						if (quantity > movieNum) {
							map.put("code", 0);
							map.put("msg", "每人最多可购买" + movieNum + "张");
							return map;
						}

						// 获取该用户当前购买的电影券数--参数需要爱动号和活动ID
						int hasNum = 0;
						if (campaign.getSelectPickUpWay() == 3) {// 虚拟码
							hasNum = mstCampaignDao.getUserBuyMoviceCount(paramMap);
						} else {
							try {

								hasNum = mstCampaignDao.getMatchNumOnePerson(paramMap);
								if (hasNum >= movieNum || (hasNum + quantity) > movieNum) {
									map.put("code", 0);
									map.put("msg", "每人最多可购买" + movieNum + "张");
									return map;
								} else {
									if (campaign.getSelectPickUpWay() == 3) {
										int movieCount = mstCampaignDao.ifHasMoviceTickets(campaignId);
										if (movieCount == 0) {
											map.put("code", 0);
											map.put("msg", "已售完");
											return map;
										}
									} else {
										// 票务总张数
										// 当前已购买张数
										int hasBuyCount = mstCampaignDao.getHasBuyTicketCount(campaignId);
										if (hasBuyCount >= allowPerson) {
											map.put("code", 0);
											map.put("msg", "已售罄");
											return map;
										} else if ((hasBuyCount + quantity) > allowPerson) {
											int num = allowPerson - hasBuyCount;
											if (num < 0) {
												num = 0;
											}
											String numStr = String.valueOf(num);
											map.put("code", 0);
											map.put("msg", "剩余数量" + numStr);
											return map;
										}
									}
								}
							} catch (Exception e) {
								// TODO: handle exception
								e.printStackTrace();
							}

						}
					}

					int selectAddressId = campaign.getSelectAddressId();
					/*
					 * if (campaign.getSelectPickUpWay() == 2 && selectAddressId
					 * == 0) { List<Map<String, Object>> addressInfo =
					 * myAddressBiz.getReceiverAddressDefault(campaign.
					 * getSelectPickUpWay()); if (addressInfo.size() > 0) {
					 * selectAddressId =
					 * Integer.valueOf(addressInfo.get(0).get("autoId").toString
					 * ()); campaign.setSelectAddressId(selectAddressId); } }
					 */
					String province, city, country, detailAddress, address = "", receiverName = "", mobilePhone = "";
					if (campaign.getSelectPickUpWay() == 2) {
						List<Map<String, Object>> addressInfo = new ArrayList<Map<String, Object>>();
						if (selectAddressId == 0) {
							addressInfo = myAddressDao.getReceiverAddressDefault(campaign.getSelectPickUpWay());
							if (addressInfo.size() > 0) {
								selectAddressId = Integer.valueOf(addressInfo.get(0).get("autoId").toString());
								campaign.setSelectAddressId(selectAddressId);
							}
						} else {
							Map<String, Object> paraMap = new HashMap<String, Object>();
							paraMap.put("autoId", selectAddressId);
							paraMap.put("idongId", idongId);
							addressInfo = myAddressDao.getReceiverAddress(paraMap);
						}
						province = addressInfo.get(0).get("province").toString();
						city = addressInfo.get(0).get("city").toString();
						country = addressInfo.get(0).get("country").toString();
						detailAddress = addressInfo.get(0).get("detailAddress").toString();
						address = province + city + country + detailAddress;
						receiverName = addressInfo.get(0).get("receiverName").toString();
						mobilePhone = addressInfo.get(0).get("mobilePhone").toString();
						campaign.setAddress(address);
						campaign.setNameAddress(receiverName);
						campaign.setMobileAddress(mobilePhone);
					}
					if (!"".equals(idongId) && idongId != null) {// 已登录
						campaign.setPickTakeDate(campaign.getBuyDate() + " " + campaign.getBuyTime());
						// 添加预约信息
						mstCampaignDao.addActivityReg(campaign);
						int subscribeId = campaign.getSubscribeId(); // 预约ID

						// 添加付款信息
						Map<String, Object> payMap = new HashMap<String, Object>();
						payMap.put("subscribeId", subscribeId);
						payMap.put("orderType", 6);// 6 是票务
						payMap.put("brandId", brandId);

						// 计算价格
						Map<String, Object> priceMap = ordergeneratedDao.getActivityPrice(subscribeId);
						double unitPrice = Double.parseDouble(priceMap.get("price").toString());
						amount = unitPrice * campaign.getQuantity();
						/*
						 * if (campaign.getSelectPickUpWay() == 2) {//快递 amount
						 * = amount + CodeConsts.Freight; payMap.put("amount",
						 * amount); }else { payMap.put("amount", amount); }
						 */
						payMap.put("amount", amount);
						ordergeneratedDao.addCampaginOrderPay(payMap);
						String orderId = payMap.get("orderId").toString();

						Map<String, Object> paraMap = new HashMap<String, Object>();
						paraMap.put("idongId", idongId);
						paraMap.put("orderId", orderId);
						paraMap.put("campaignId", campaignId);
						if (campaign.getSelectPickUpWay() == 3) {
							// 改电影券为已使用 --被该用户购买
							mstCampaignDao.setMovieTicketsUse(paraMap);
						}

						map.put("code", 1);
						List<Map<String, Object>> payInfo = getPayInfo(mstCampaignDao.getPayInfo(campaignId));
						map.put("zhifubao", payInfo.get(0).get("zhifubao"));
						map.put("weixin", payInfo.get(1).get("weixin"));
						map.put("voucher", payInfo.get(2).get("voucher"));
						map.put("exchange", payInfo.get(3).get("exchange"));
						map.put("membership", payInfo.get(4).get("membership"));
						map.put("discount", payInfo.get(5).get("discount"));
						Map<String, Object> resultMap = new HashMap<String, Object>();
						resultMap.put("subscribeId", 0);
						resultMap.put("orderId", orderId);
						resultMap.put("quantity", campaign.getQuantity());
						resultMap.put("amount", amount);
						resultMap.put("foodName", "");
						map.put("result", resultMap);
						return map;

					} else {// 未登录
							// 判断该手机号码是否已注册
						int count = mstCampaignDao.getIfRegister(contactPhone);
						if (count <= 0) { // 未注册
							// 先注册新用户，产生爱动号
							Map<String, Object> regMap = new HashMap<String, Object>();
							regMap.put("contactName", contactName);
							regMap.put("contactPhone", contactPhone);
							mstCampaignDao.addMemberReg(regMap);
							// 注册成功后产生爱动号
							String regIdongId = regMap.get("idongId").toString();
							campaign.setIdongId(regIdongId);

							// 添加预约信息
							mstCampaignDao.addActivityReg(campaign);
							int subscribeId = campaign.getSubscribeId(); // 预约ID

							// 添加付款信息
							Map<String, Object> payMap = new HashMap<String, Object>();
							payMap.put("subscribeId", subscribeId);
							payMap.put("orderType", 6);

							// 计算价格
							Map<String, Object> priceMap = ordergeneratedDao.getActivityPrice(subscribeId);
							double unitPrice = Double.parseDouble(priceMap.get("price").toString());
							amount = unitPrice * campaign.getQuantity();
							/*
							 * if (campaign.getSelectPickUpWay() == 2) {//快递
							 * amount = amount + CodeConsts.Freight;
							 * payMap.put("amount", amount); }else {
							 * payMap.put("amount", amount); }
							 */

							payMap.put("amount", amount);
							ordergeneratedDao.addCampaginOrderPay(payMap);
							String orderId = payMap.get("orderId").toString();

							Map<String, Object> paraMap = new HashMap<String, Object>();
							paraMap.put("idongId", campaign.getIdongId());
							paraMap.put("orderId", orderId);
							paraMap.put("campaignId", campaignId);
							if (campaign.getSelectPickUpWay() == 3) {
								// 改电影券为已使用 --被该用户购买
								mstCampaignDao.setMovieTicketsUse(paraMap);
							}

							map.put("code", 1);
							List<Map<String, Object>> payInfo = getPayInfo(mstCampaignDao.getPayInfo(campaignId));
							map.put("zhifubao", payInfo.get(0).get("zhifubao"));
							map.put("weixin", payInfo.get(1).get("weixin"));
							map.put("voucher", payInfo.get(2).get("voucher"));
							map.put("exchange", payInfo.get(3).get("exchange"));
							map.put("membership", payInfo.get(4).get("membership"));
							map.put("discount", payInfo.get(5).get("discount"));
							Map<String, Object> resultMap = new HashMap<String, Object>();
							resultMap.put("subscribeId", 0);
							resultMap.put("orderId", orderId);
							resultMap.put("quantity", campaign.getQuantity());
							resultMap.put("amount", amount);
							resultMap.put("foodName", "");
							map.put("result", resultMap);

							return map;
						} else { // 已注册
							int rsultIdongId = mstCampaignDao.getIdongIdByPhone(contactPhone);
							campaign.setIdongId(rsultIdongId + "");
							// 添加预约信息
							mstCampaignDao.addActivityReg(campaign);
							int subscribeId = campaign.getSubscribeId(); // 预约ID
							// 添加付款信息
							Map<String, Object> payMap = new HashMap<String, Object>();
							payMap.put("subscribeId", subscribeId);
							payMap.put("orderType", 6);

							// 计算价格
							Map<String, Object> priceMap = ordergeneratedDao.getActivityPrice(subscribeId);
							double unitPrice = Double.parseDouble(priceMap.get("price").toString());
							amount = unitPrice * campaign.getQuantity();
							/*
							 * if (campaign.getSelectPickUpWay() == 2) {//快递
							 * amount = amount + CodeConsts.Freight;
							 * payMap.put("amount", amount); }else {
							 * payMap.put("amount", amount); }
							 */
							payMap.put("amount", amount);
							ordergeneratedDao.addCampaginOrderPay(payMap);
							String orderId = payMap.get("orderId").toString();

							Map<String, Object> paraMap = new HashMap<String, Object>();
							paraMap.put("idongId", campaign.getIdongId());
							paraMap.put("orderId", orderId);
							paraMap.put("campaignId", campaignId);
							if (campaign.getSelectPickUpWay() == 3) {
								// 改电影券为已使用 --被该用户购买
								mstCampaignDao.setMovieTicketsUse(paraMap);
							}

							map.put("code", 1);
							List<Map<String, Object>> payInfo = getPayInfo(mstCampaignDao.getPayInfo(campaignId));
							map.put("zhifubao", payInfo.get(0).get("zhifubao"));
							map.put("weixin", payInfo.get(1).get("weixin"));
							map.put("voucher", payInfo.get(2).get("voucher"));
							map.put("exchange", payInfo.get(3).get("exchange"));
							map.put("membership", payInfo.get(4).get("membership"));
							map.put("discount", payInfo.get(5).get("discount"));
							Map<String, Object> resultMap = new HashMap<String, Object>();
							resultMap.put("subscribeId", 0);
							resultMap.put("orderId", orderId);
							resultMap.put("quantity", campaign.getQuantity());
							resultMap.put("amount", amount);
							resultMap.put("foodName", "");
							map.put("result", resultMap);
							return map;
						}
					}
				}
			} else {
				map.put("code", 0);
				map.put("msg", "参数缺失");
				return map;
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", 0);
			map.put("msg", "系统异常");
			return map;
		}
		return map;
	}
	
	/**
	 * 得到支付信息
	 * 
	 * @param campaignId
	 * @return
	 */
	public List<Map<String, Object>> getPayInfo(String payInfo) {
		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
		Map<String, Object> zhifubao = new HashMap<String, Object>();
		Map<String, Object> weixin = new HashMap<String, Object>();
		Map<String, Object> voucher = new HashMap<String, Object>();
		Map<String, Object> exchange = new HashMap<String, Object>();
		Map<String, Object> membership = new HashMap<String, Object>();
		Map<String, Object> discount = new HashMap<String, Object>();
		if (payInfo.contains("1")) {
			zhifubao.put("zhifubao", true);
			result.add(zhifubao);
		} else {
			zhifubao.put("zhifubao", false);
			result.add(zhifubao);
		}
		if (payInfo.contains("2")) {
			weixin.put("weixin", true);
			result.add(weixin);
		} else {
			weixin.put("weixin", false);
			result.add(weixin);
		}
		if (payInfo.contains("3")) {
			voucher.put("voucher", true);
			result.add(voucher);
		} else {
			voucher.put("voucher", false);
			result.add(voucher);
		}
		if (payInfo.contains("4")) {
			exchange.put("exchange", true);
			result.add(exchange);
		} else {
			exchange.put("exchange", false);
			result.add(exchange);
		}
		if (payInfo.contains("5")) {
			membership.put("membership", true);
			result.add(membership);
		} else {
			membership.put("membership", false);
			result.add(membership);
		}
		if (payInfo.contains("6")) {
			discount.put("discount", true);
			result.add(discount);
		} else {
			discount.put("discount", false);
			result.add(discount);
		}
		return result;
	}
	/**
	 * 根据爱动号查看是否预约幸运抽奖
	 * @param idongId
	 * @return
	 */
	public int getLuckyDrawCount(int idongId){
		return mstCampaignDao.getLuckyDrawCount(idongId);
	}
	/**
	 * 获取该人该天共消费金额
	 * @param map
	 * @return
	 */
	public double getActuallyCount(Map<String,Object> map){
		return mstCampaignDao.getActuallyCount(map);
	}
	/**
	 * 获取最近要抽奖的开始时间
	 * @return
	 */
	public List<Map<String,Object>> getLuckyDrawTime(){
		return mstCampaignDao.getLuckyDrawTime();
	}
	/**
	 * 获取幸运抽奖人
	 * @return
	 */
	public List<Map<String,Object>> getLuckyDrawList(){
		return mstCampaignDao.getLuckyDrawList();
	}
	/**
	 * 人列表
	 * @return
	 */
	public List<Map<String,Object>> getLuckyDrawPersonList(Map<String,Object> map){
		return mstCampaignDao.getLuckyDrawPersonList(map);
	}
	/**
	 * 获取距离当前日期最近的场次
	 */
	public Map<String,Object> getLastLuckyDraw(){
		return mstCampaignDao.getLastLuckyDraw();
	}
}
